package com.guilin.controller;

import com.guilin.common.core.controller.BaseController;
import com.guilin.common.core.domain.AjaxResult;
import com.guilin.common.core.page.TableDataInfo;
import com.guilin.common.utils.SecurityUtils;
import com.guilin.common.utils.poi.ExcelUtil;
import com.guilin.domain.ForumThread;
import com.guilin.system.service.ForumLikeService;
import com.guilin.system.service.ForumThreadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛主题Controller
 *
 * @author xiaoqi
 * @date 2025-06-20
 */
@RestController
@RequestMapping("/admin-management/forumthread")
public class ForumThreadController extends BaseController
{
    @Autowired
    private ForumThreadService forumThreadService;

    @Autowired
    private ForumLikeService forumLikeService;

    /**
     * 查询论坛主题列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumthread:list')")
    @GetMapping("/list")
    public TableDataInfo list(ForumThread forumThread)
    {
        startPage();
        List<ForumThread> list = forumThreadService.selectForumThreadList(forumThread);
        return getDataTable(list);
    }

    /**
     * 导出论坛主题列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumthread:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumThread forumThread)
    {
        List<ForumThread> list = forumThreadService.selectForumThreadList(forumThread);
        ExcelUtil<ForumThread> util = new ExcelUtil<ForumThread>(ForumThread.class);
        util.exportExcel(response, list, "论坛主题数据");
    }

    /**
     * 获取论坛主题详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumthread:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        // 增加浏览量
        forumThreadService.incrementViewCount(id);

        // 查询论坛主题详细信息
        ForumThread forumThread = forumThreadService.selectForumThreadById(id);

        // 获取当前用户点赞状态
        boolean hasLiked = false;
        try {
            Long userId = SecurityUtils.getUserId();
            if (userId != null) {
                // 使用新创建的ForumLikeService
                hasLiked = forumLikeService.hasLiked(id, userId);
            }
        } catch (Exception e) {
            // 忽略异常，保持hasLiked为false
        }

        // 创建返回对象
        Map<String, Object> result = new HashMap<>();
        result.put("thread", forumThread);
        result.put("hasLiked", hasLiked);

        return AjaxResult.success(result);
    }

    /**
     * 新增论坛主题
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumthread:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ForumThread forumThread)


    {

        String username = SecurityUtils.getLoginUser().getUsername();
        forumThread.setUserName(username);
        return toAjax(forumThreadService.insertForumThread(forumThread));
    }

    /**
     * 修改论坛主题
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumthread:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ForumThread forumThread)
    {
        return toAjax(forumThreadService.updateForumThread(forumThread));
    }

    /**
     * 删除论坛主题
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumthread:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(forumThreadService.deleteForumThreadByIds(ids));
    }
}
